package com.codewsq;

import com.alibaba.fastjson.JSONArray;

import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;

public class MapAreaPoint {


    public static void main(String[] args) {
        ArrayList<LatLng> bound = new ArrayList<>();
        String address = "[[121.271506,31.270237],[121.177436,31.259673],[121.084052,31.240888],[121.086799,31.142207],[120.894538,31.106939],[120.982429,31.017535],[121.067573,30.854981],[121.240607,30.878557],[121.339484,30.918622],[121.339484,30.918622]]";
        List<JSONArray> jsons = JSONArray.parseArray(address, JSONArray.class);
        for (JSONArray json : jsons) {
            String lng = json.get(1).toString();
            String lat = json.get(0).toString();
            bound.add(new LatLng(Double.parseDouble(lng), Double.parseDouble(lat)));
        }
        //  121.147751   31.180597
        //  121.507753   31.234330
        boolean inPolygon = isInPolygon(bound, 31.234330, 121.507753 );
        System.out.println(inPolygon);
    }


    public static boolean isInPolygon(ArrayList<LatLng> bound, double pointlng, double pointLat) {
        Point2D.Double point = new Point2D.Double(pointlng, pointLat);
        List<Point2D.Double> pointList = new ArrayList<>();
        for (int i = 0; i < bound.size(); i++) {
            pointList.add(new Point2D.Double(bound.get(i).lng, bound.get(i).lat));
        }
        return check(point, pointList);
    }


    public static boolean check(Point2D.Double _point, List<Point2D.Double> polygon) {
        java.awt.geom.GeneralPath peneralPath = new java.awt.geom.GeneralPath();

        Point2D.Double first = polygon.get(0);
        peneralPath.moveTo(first.x, first.y);
        polygon.remove(0);

        for (Point2D.Double d : polygon) {
            peneralPath.lineTo(d.x, d.y);
        }

        peneralPath.lineTo(first.x, first.y);
        peneralPath.closePath();

        return peneralPath.contains(_point);
    }






    /* *//**
     * 某个点是否在区域内
     * @param aMap 地图元素
     * @param latLngList 区域坐标合集
     * @param latLng 需要判断的点
     * @return
     *//*
    public static boolean polygonCon(AMap aMap, List<LatLng> latLngList ,LatLng latLng ) {
        PolygonOptions options = new PolygonOptions();
        for (LatLng i : latLngList) {
            options.add(i);
        }
        options.visible(false); //设置区域是否显示
        Polygon polygon = aMap.addPolygon(options);
        boolean contains = polygon.contains(latLng);
        polygon.remove();
        return contains;
    }*/
}
